package cn.supermaster.management.web.service.monitor.impl;

import cn.supermaster.management.common.response.WebResponse;
import cn.supermaster.management.core.web.service.CacheService;
import cn.supermaster.management.web.service.monitor.IMonitorCacheService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;

import java.util.Set;

/**
 * All rights Reserved, Designed By www.super-bytecode.com
 *
 * @ProjectName: supermaster-management
 * @Package: cn.supermaster.management.web.service.monitor.impl
 * @ClassName: MonitorCacheServiceImpl
 * @Description: []
 * <p> </p>
 * @Author: [Lance Ting]
 * @Date: 2021/1/10 16:52
 * @Version: V1.0
 * @Copyright: 2021 www.super-bytecode.comn Inc. All rights reserved.
 * TODO: 注意,本文件Lance Ting所作,如果转载或使用请标明具体出处!
 **/
@Service
public class MonitorCacheServiceImpl implements IMonitorCacheService {
    /*** URI前缀 **/
    private String prefix = "monitor/cache";

    @Autowired
    private CacheService cacheService;

    /*
     * ================================================
     *  页面跳转功能实现
     * ================================================
     */

    /**
     * @Description: [获取所有缓存]
     * <p></P>
     * @Title: queryAllCacheNames
     * @Author: Lance Ting
     * @Date: 2021-01-10
     * @Param: modelMap
     * @Return: java.lang.String
     * @Throws:
     */
    @Override
    public String queryAllCacheNames(ModelMap modelMap) {
        String[] cacheNames = cacheService.getCacheNames();
        modelMap.put("cacheNames", cacheNames);
        return prefix + "/cache";
    }

    /**
     * @Description: [获取缓存名称]
     * <p></P>
     * @Title: queryCacheNames
     * @Author: Lance Ting
     * @Date: 2021-01-10
     * @Param: modelMap
     * @Param: fragment
     * @Return: java.lang.String
     * @Throws:
     */
    @Override
    public String queryCacheNames(ModelMap modelMap, String fragment) {
        // 获取缓存名称
        String[] cacheNames = cacheService.getCacheNames();
        modelMap.put("cacheNames", cacheNames);
        return prefix + "/cache::" + fragment;
    }

    /**
     * @Description: []
     * <p></P>
     * @Title: queryCacheKeys
     * @Author: Lance Ting
     * @Date: 2021-01-10
     * @Param: modelMap
     * @Param: fragment
     * @Param: cacheName
     * @Return: java.lang.String
     * @Throws:
     */
    @Override
    public String queryCacheKeys(ModelMap modelMap, String fragment, String cacheName) {
        Set<String> cacheKeys = cacheService.getCacheKeys(cacheName);
        //
        modelMap.put("cacheName", cacheName);
        modelMap.put("cacheKyes", cacheKeys);
        return prefix + "/cache::" + fragment;
    }

    /**
     * @Description: []
     * <p></P>
     * @Title: queryCacheValue
     * @Author: Lance Ting
     * @Date: 2021-01-10
     * @Param: modelMap
     * @Param: fragment
     * @Param: cacheName
     * @Param: cacheKey
     * @Return: java.lang.String
     * @Throws:
     */
    @Override
    public String queryCacheValue(ModelMap modelMap, String fragment, String cacheName, String cacheKey) {
        Object cacheValue = cacheService.getCacheValue(cacheName, cacheKey);
        //
        modelMap.put("cacheName", cacheName);
        modelMap.put("cacheKey", cacheKey);
        modelMap.put("cacheValue", cacheValue);
        return prefix + "/cache::" + fragment;
    }
    /*
     * ================================================
     *  Rest功能实现
     * ================================================
     */

    /**
     * @Description: [清空缓存]
     * <p></P>
     * @Title: doClearCacheName
     * @Author: Lance Ting
     * @Date: 2021-01-10
     * @Param: modelMap
     * @Param: cacheName
     * @Return: cn.supermaster.management.common.response.WebResult
     * @Throws:
     */
    @Override
    public WebResponse doClearCacheName(ModelMap modelMap, String cacheName) {
        cacheService.clearCacheName(cacheName);
        return WebResponse.success();
    }

    /**
     * @Description: [清空缓存Key]
     * <p></P>
     * @Title: doClearCacheKey
     * @Author: Lance Ting
     * @Date: 2021-01-10
     * @Param: modelMap
     * @Param: cacheName
     * @Param: cacheKey
     * @Return: cn.supermaster.management.common.response.WebResult
     * @Throws:
     */
    @Override
    public WebResponse doClearCacheKey(ModelMap modelMap, String cacheName, String cacheKey) {
        cacheService.clearCacheKey(cacheName, cacheKey);
        return WebResponse.success();
    }

    /**
     * @param modelMap
     * @Description: [清空缓存]
     * <p></P>
     * @Title: doClearCacheAll
     * @Author: Lance Ting
     * @Date: 2021-01-10
     * @Param: modelMap
     * @Return: cn.supermaster.management.common.response.WebResult
     * @Throws:
     */
    @Override
    public WebResponse doClearCacheAll(ModelMap modelMap) {
        cacheService.clearAll();
        return WebResponse.success();
    }
}